#include "csreader.h"



CSReader::CSReader()
{

}

CSReader s_reader;

CSReader * CSReader::shareReader()
{
	return &s_reader;
}



int CSReader::readCSCProjFile(char * filename)
{
	xmlDocPtr doc;  
	xmlNodePtr cur;  

	xmlKeepBlanksDefault(0);

	doc = xmlReadFile(filename ,NULL, XML_PARSE_NOBLANKS);  

	if(doc == NULL) {  
		fprintf(stderr, "doc error!\n");  
		return 0;  
	}  

	cur = xmlDocGetRootElement(doc);  

	if(cur == NULL) {  
		fprintf(stderr, "root error!\n");  
		xmlFreeDoc(doc);  
		return 0;  
	}  

	if(xmlStrcmp(cur->name, (const xmlChar*)"CCScene")) {  
		printf("end\n");  
		return 0;  
	}  

	MainWindow *mainwindow =ConstantManager::sharedManager()->qWindow();
	TreeWidget_CCScene * treeWidget = qFindChild<TreeWidget_CCScene*>(mainwindow , "treeWidget_nodes");

	for (cur= cur->children; cur!=NULL ;cur = cur->next)
	{
		iteratorXmlNodes(cur , 0 , 0 ,treeWidget);
	}

	xmlFreeDoc(doc);  
	return 0;  

}

bool CSReader::nodeStartWith(xmlNodePtr node, char * ccnodeName)
{
	return xmlStrncmp(node->name , (xmlChar*) ccnodeName , strlen(ccnodeName))==0;
}


 

void CSReader::findChildren(xmlNodePtr node,QTreeWidgetItem * parentItem ,CCNode * parentCNode , TreeWidget_CCScene *treeWidget )
{
	if (nodeStartWith(node,"CC"))
	{
		iteratorXmlNodes(node , parentItem , parentCNode ,  treeWidget);
	}
}



void CSReader::iteratorXmlNodes( xmlNodePtr node , QTreeWidgetItem * parentItem , CCNode * parentCNode , TreeWidget_CCScene *treeWidget )
{
	if (node->type == XML_TEXT_NODE)
		return;
	if (nodeStartWith(node , "CC"))
	{
		char * curNodeName = (char*)node->name;
		XmlNodeMap childrenNodeMap = XmlNodeMap::readFromXmlNode(node);
		vector <char*> nameList = childrenNodeMap.getNodeNameList();
		QCocoNode qnode = CocoHieriachy::sharedHieriachy()->getCoCoNodeById( atoi((char*)xmlGetProp(node , (xmlChar*)"id")));
		// get PEditors.
		QTreeNode <QCocoNode> * treeNode = CocoHieriachy::sharedHieriachy()->getTreeNodeById(qnode.id);
		QVector<QCocoNode> superNodes =  treeNode->getSupertypes();
		QTreeWidgetItem * treeWidgetItem ; 
		if (parentItem == 0 )
		{
			treeWidgetItem = new QTreeWidgetItem(treeWidget);
			
		}else
			treeWidgetItem = new QTreeWidgetItem(parentItem);
		
		QCocoNode * newqnode = treeWidget->addNode(qnode , treeWidgetItem ,  parentCNode );

		for (int i = superNodes.size() -1;i >=0 ; i --)
		{
			// get peditor by name .
			PEditor * peditor = PEFactory::shareFactory()->getPEditorByName(superNodes.at(i).name);
			if(peditor == NULL )continue;

			for (int j = 0 ;j <nameList.size(); j++)
			{
				//current NodeName ; 
				// 
				char * curChildNodeName  = nameList[j];
				//get QtVariantProperty 
				QtVariantProperty *qproperty = peditor->Property(curChildNodeName);
				if(qproperty == NULL) continue;
				peditor->initializeProperty(qproperty);
				//get node's type attribute.
				if (!childrenNodeMap.hasAttribute(curChildNodeName , "type"))continue;
				int type = childrenNodeMap.getNodeAttributeInt(curChildNodeName, "type");
				if (type == TYPE_ENUMETYPE)
				{
 					QStringList enumList = qproperty->attributeValue("enumNames").value<QStringList>();
// 					std::string imagePath = enumList[qproperty->value().value<int>()].toLocal8Bit().data();
					int imageIndex= enumList.indexOf(childrenNodeMap.getNodeContent(curChildNodeName));

					qproperty->setValue(imageIndex);
					continue;
				}
				switch (type)
				{
				case QVariant::String :
					
					qproperty->setValue(childrenNodeMap.getNodeContent(curChildNodeName) );
					
					break;
				case QVariant::Int :
					qproperty->setValue(childrenNodeMap.getNodeContentInt(curChildNodeName));
					break;
				case QVariant::Double :
					qproperty->setValue(childrenNodeMap.getNodeContentFloat(curChildNodeName));
					break;
				case QVariant::PointF:
					{
						float x = childrenNodeMap.getNodeAttributeFloat(curChildNodeName , "x");
						float y = childrenNodeMap.getNodeAttributeFloat(curChildNodeName , "y");
						QPointF mypoiontf ( x, y);
						qproperty->setValue(mypoiontf);
						break;
					}
				case QVariant::SizeF:
					{
						float w = childrenNodeMap.getNodeAttributeFloat(curChildNodeName , "width");
						float h = childrenNodeMap.getNodeAttributeFloat(curChildNodeName , "height");
						QSizeF mysizef ( w, h );
						qproperty->setValue(mysizef);
						break;
					}
				case QVariant::Color:
					{
						float r = childrenNodeMap.getNodeAttributeFloat(curChildNodeName , "r");
						float g = childrenNodeMap.getNodeAttributeFloat(curChildNodeName , "g");
						float b = childrenNodeMap.getNodeAttributeFloat(curChildNodeName , "b");
						float a = childrenNodeMap.getNodeAttributeFloat(curChildNodeName , "a");
						QColor color(r,g,b ,a );
						qproperty->setValue(color);
						break;
					}
				case QVariant::Bool:
					{
						bool node_bol = childrenNodeMap.getNodeContentBool(curChildNodeName);

						qproperty->setValue(QVariant(node_bol));
						break;
					}
				default:
					break;
				}
			}
		}
		for (xmlNodePtr childNode = node->children ; childNode!=NULL ;childNode = childNode->next)
		{
			findChildren(childNode , treeWidgetItem , newqnode->m_node ,treeWidget);
		}
	}
}

